import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';
import 'package:get/get.dart';

import 'package:cbec_app/components/common/common_style.dart';
import 'package:cbec_app/components/order/after_goods_item.dart';
import 'package:cbec_app/pages/my/controller/after_pop_ctr.dart';
import 'package:cbec_app/routes/routes.dart';
import 'package:cbec_app/utils/Tools.dart';

late int currentType = 0;

late List goodsLists = [];

class AfterSalesPopup extends StatefulWidget {
  final int afterSaleStyle;

  const AfterSalesPopup({
    super.key,
    required this.afterSaleStyle,
  });

  @override
  State<AfterSalesPopup> createState() => _AfterSalesPopupState();
}

class _AfterSalesPopupState extends State<AfterSalesPopup> {
  late List typeList = widget.afterSaleStyle == 1 ? ["退款退货", "换货", "部分退款"] : ["退款退货"];

  final AfterPopController afterPopCtr = Get.put(AfterPopController());

  @override
  void initState() {
    super.initState();
    currentType = 0;
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Container(
          decoration: border_radis(Colors.white),
          child: Column(
            children: [
              SizedBox(
                height: 18.h,
              ),
              Text("请选择售后类型", style: setTextStyle(15, FontWeight.w500, Tools.fromHex("#333333"))),
              SizedBox(
                height: 20.h,
              ),
              Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  //typeList.map((item) => _topButtonItem(item)).toList()
                  children: [
                    ...(typeList.asMap().entries.map((item) {
                      return _topButtonItem(item.value, item.key);
                    })),
                  ]),
              SizedBox(height: 15.h),
              Container(
                  height: 230.h,
                  padding: EdgeInsets.only(left: 18.w, right: 22.w),
                  child: ListView.builder(
                      itemCount: goodsLists.length,
                      shrinkWrap: true,
                      itemBuilder: (context, index) {
                        print('每一个 ==== ${goodsLists[index]}');
                        return widget.afterSaleStyle == 1
                            ? AfterGoodsItem(
                                item: goodsLists[index],
                              )
                            : AfterGoodsItem2(
                                item: goodsLists[index],
                              );
                      })),
              //按钮
              Container(
                padding: EdgeInsets.fromLTRB(18.w, 5.w, 18.w, 5.w),
                child: solidButton("下一步", () {
                  afterPopCtr.next(currentType);
                }),
              )
            ],
          )),
    );
  }

  Widget _topButtonItem(String txt, int index) {
    Color buttonColor = Colors.white;
    Color txtColor = Tools.fromHex("#333333");

    if (index == currentType) {
      buttonColor = Colors.red;
      txtColor = Colors.white;
    }

    return InkWell(
      onTap: widget.afterSaleStyle == 1
          ? () => {
                setState(() {
                  currentType = index;
                  afterPopCtr.init();
                })
              }
          : null,
      child: Container(
        alignment: Alignment.center,
        decoration: border_radis(buttonColor, borderColor: Tools.fromHex("#F7F7F7"), radiusValue: 20),
        width: 100.w,
        height: 35.h,
        child: Text(txt, style: setTextStyle(13, FontWeight.w500, txtColor)),
      ),
    );
  }
}

void afterSalesPopup(
  BuildContext context,
  List goodsList,
  String orderId, {
  required int afterSaleStyle, // 命名参数
}) {
  final AfterPopController afterPopCtl = Get.put(AfterPopController());
  afterPopCtl.afterSaleStyle = afterSaleStyle;
  afterPopCtl.init();
  afterPopCtl.setOrderId(orderId);
  goodsLists = goodsList;
  print('LELELEL  $goodsList');
  showModalBottomSheet(
    context: context,
    builder: (BuildContext context) {
      return AfterSalesPopup(afterSaleStyle: afterSaleStyle);
    },
  );
}
